/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import presentation.controller.ControlPanelMyAccount;
import presentation.controller.ControlPanelRegister;
import vo.StudentVO;

/**
 *
 * @author Joanna 8a
 */
public class PanelRegister extends javax.swing.JPanel {

    /**
     * Creates new form PanelRegister
     */
    public PanelRegister(MainFrame parent, String accion, AuxiliarFrame aux) {
        this.parent = parent;
        this.aux = aux;
        parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Smiley1.png")).getImage());
        initComponents();
        jTextFieldUsername.requestFocusInWindow();
        if(accion.equals("Modificar")){
            jButtonRegister.setText(accion);
            jTextFieldUsername.setEditable(false);
            llenarCampos();
            
        }
        jDateChooserBirthDate.setEnabled(false);
        jDateChooserBirthDate.getCalendarButton().setEnabled(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButtonBack = new javax.swing.JButton();
        jButtonRegister = new javax.swing.JButton();
        jPasswordField2 = new javax.swing.JPasswordField();
        jComboBoxCareer = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldLastName = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jDateChooserBirthDate = new com.toedter.calendar.JDateChooser();

        jButtonBack.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonBack.setText("Volver");
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jButtonRegister.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonRegister.setText("Registrar");
        jButtonRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegisterActionPerformed(evt);
            }
        });

        jPasswordField2.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jPasswordField2.setText("jPasswordField2");
        jPasswordField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordField2FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField2FocusLost(evt);
            }
        });

        jComboBoxCareer.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jComboBoxCareer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ingeniería de Sistemas", "Ingeniería Electrónica", "Ingeniería Civil", "Ingeniería Eléctrica", "Ingerniería Ambiental", "Ingeniería Química", "Derecho", "Ciencias Políticas", "Trabajo Social", "Biología", "Farmacia", "Enfermería", "Física", "Química", "Matemáticas" }));

        jLabel3.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel3.setText("Confirma contraseña");

        jLabel4.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel4.setText("Nombre");

        jTextFieldName.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel5.setText("Apellidos");

        jTextFieldLastName.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel6.setText("Fecha de nacimiento");

        jLabel7.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel7.setText("Carrera");

        jPasswordField1.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jPasswordField1.setText("jPasswordField1");
        jPasswordField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordField1FocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField1FocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel2.setText("Contraseña");

        jTextFieldUsername.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabel1.setText("Usuario");

        jDateChooserBirthDate.setDate(restriccionFecha(16));
        jDateChooserBirthDate.setDateFormatString("dd/MM/yyyy");
        jDateChooserBirthDate.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jDateChooserBirthDate.setMaxSelectableDate(restriccionFecha(14));
        jDateChooserBirthDate.setMinSelectableDate(restriccionFecha(100));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonRegister)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonBack))
                    .addComponent(jLabel7)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldUsername)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                    .addComponent(jPasswordField2)
                    .addComponent(jTextFieldName)
                    .addComponent(jTextFieldLastName)
                    .addComponent(jComboBoxCareer, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jDateChooserBirthDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addGap(5, 5, 5)
                .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxCareer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRegister)
                    .addComponent(jButtonBack))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        if(jButtonRegister.getText().equals("Registrar")){
            parent.cambiarPanel(new PanelLogin(parent));
        }
        else{
            aux.dispose();
        }
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jButtonRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegisterActionPerformed
        StudentVO vo = new StudentVO();
        javax.swing.JButton boton = (javax.swing.JButton) evt.getSource();
        
        vo.setUsername(jTextFieldUsername.getText());
        vo.setName(jTextFieldName.getText());
        vo.setLastName(jTextFieldLastName.getText());
        vo.setPassword(new String(jPasswordField1.getPassword()));
        vo.setCareer(jComboBoxCareer.getSelectedItem().toString());
        vo.setBirthDate(jDateChooserBirthDate.getDate());
        vo.setActiveUser(new ControlPanelRegister().colocarIpUsuario() + ".true");

        ControlPanelRegister cpr = new ControlPanelRegister();
        String mg = "";
        if (boton.getText().equals("Registrar")) {
            mg = cpr.registrar(vo, new String(jPasswordField2.getPassword()));
        } 
        else if(boton.getText().equals("Modificar")){
            mg = cpr.modificarAlumno(vo, new String(jPasswordField2.getPassword()));
        }
        JOptionPane.showMessageDialog(this, mg, "Aviso", JOptionPane.INFORMATION_MESSAGE, null);
        if (mg.equals(cpr.ERRORPSWRD)) {
            jPasswordField1.requestFocus();
            jPasswordField1.selectAll();
        } else if (mg.equals(cpr.OK)) {
            parent.cambiarPanel(MainPanel.getInstance());
            parent.controlBarraHerramientas(true, false);
            parent.setResizable(true);
            //TODO Dialogo para crear primer horario
            int option = JOptionPane.showConfirmDialog(null, "Desea crear un Horario de claes?", "Recomendacion", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                AuxiliarFrame aux2 = new AuxiliarFrame();
                aux2.cambiarPanel(new PanelSearchSubjects(aux2, PanelSearchSubjects.CREATE_SCHEDULE));
                aux2.setVisible(true);
            }
        } else if (mg.equals(cpr.USEREXIST)) {
            jTextFieldUsername.requestFocus();
            jTextFieldUsername.selectAll();
        }
        else if(mg.equals(cpr.OK_MODIFICACION)){
            aux.dispose();
            parent.getjMenuBar1().setVisible(true);
        }
        
    }//GEN-LAST:event_jButtonRegisterActionPerformed

    private void jPasswordField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField1FocusGained
        String text = String.valueOf(jPasswordField1.getPassword());
        if (text.equals("jPasswordField1")) {
            jPasswordField1.setText(null);
        }
    }//GEN-LAST:event_jPasswordField1FocusGained

    private void jPasswordField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField1FocusLost
        if (jPasswordField1.getPassword().length == 0) {
            jPasswordField1.setText("jPasswordField1");
        }
    }//GEN-LAST:event_jPasswordField1FocusLost

    private void jPasswordField2FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField2FocusGained
        String text = String.valueOf(jPasswordField2.getPassword());
        if (text.equals("jPasswordField2")) {
            jPasswordField2.setText(null);
        }
    }//GEN-LAST:event_jPasswordField2FocusGained

    private void jPasswordField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField2FocusLost
         if (jPasswordField2.getPassword().length == 0) {
            jPasswordField2.setText("jPasswordField2");
        }
    }//GEN-LAST:event_jPasswordField2FocusLost
    
    public Date restriccionFecha (int restriccion){
        GregorianCalendar g = new GregorianCalendar();
        g.set(Calendar.YEAR, g.get(Calendar.YEAR) - restriccion);
        return g.getTime();
    }
    
    private void llenarCampos(){
        StudentVO al = new ControlPanelMyAccount().findByIp();
        
        jTextFieldUsername.setText(al.getUsername());
        jPasswordField1.setText(al.getPassword());
        jPasswordField2.setText(al.getPassword());
        jTextFieldName.setText(al.getName());
        jTextFieldLastName.setText(al.getLastName());
        jDateChooserBirthDate.getDateEditor().setDate(al.getBirthDate());
        jComboBoxCareer.setSelectedItem(al.getCareer());
    }
    
    private AuxiliarFrame aux;
    private MainFrame parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBack;
    private javax.swing.JButton jButtonRegister;
    private javax.swing.JComboBox jComboBoxCareer;
    private com.toedter.calendar.JDateChooser jDateChooserBirthDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextFieldLastName;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
}
